package com.kly.cashmall.module.splash;

import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.gyf.immersionbar.ImmersionBar;
import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.ProtocolBean;
import com.kly.cashmall.bean.UpdateBean;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.splash.presenter.SplashPresenter;
import com.kly.cashmall.module.splash.presenter.SplashViewer;
import com.kly.cashmall.popup.SystemTipWithTitlePopup;
import com.kly.cashmall.services.UserInfoHelper;

/**
 * @author
 */
public class SplashActivity extends BaseBarActivity implements SplashViewer {

    @PresenterLifeCycle
    SplashPresenter presenter = new SplashPresenter(this);

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
        setToolbarVisible(false);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        if (UserInfoHelper.getInstance().isPrivacyIsShow()) {
            presenter.getAppCode();
        } else {
            presenter.getProtocol();
        }
    }

    @Override
    protected void loadData() {

    }

    private boolean isReject = false;

    private void showPrivacyPop(String registerProtocol, String privacyPolicy) {
        SystemTipWithTitlePopup popup = new SystemTipWithTitlePopup(this);
        popup.setOutSideDismiss(false);
        popup.setBackPressEnable(false);
        popup.setPopupGravity(Gravity.CENTER).showPopupWindow();
        popup.setTitleText(getString(R.string.pop_common_title_hint));
        popup.setConfirmText(getString(R.string.system_message_confirm));
        popup.setCancelText(getString(R.string.system_message_cancel));
        popup.setPrivacyText(getString(R.string.splash_guide_text), registerProtocol, privacyPolicy);
        popup.setConfirmClick(view1 -> {
            UserInfoHelper.getInstance().setPrivacyIsShow(true);
            presenter.getAppCode();
            popup.dismiss();
        });
        popup.setCancelClick(view1 -> {
            if (isReject) {
                popup.dismiss();
                finish();
                return;
            }
            isReject = true;
            popup.setCancelText(getString(R.string.do_not_agree_and_quit_hint));
            popup.setPrivacyText(getString(R.string.splash_guide_refuse_text), registerProtocol, privacyPolicy);
        });
    }


    private void showErrorUi() {
        LinearLayout layoutNetError = findViewById(R.id.net_error_layout_id);
        ImageView errorCallCustomer = findViewById(R.id.error_call_customer);
        errorCallCustomer.setOnClickListener(v -> {
//            BaseActionHelper.with(this).handleAction(ActionBuildHelper.with(ActionHeads.TEL).put("tel", "+917677112200").getAction());
        });
        layoutNetError.setVisibility(View.VISIBLE);
        layoutNetError.setOnClickListener(v -> {
            presenter.getAppCode();
        });
    }

    @Override
    public void getAppCodeSuccess(UpdateBean appCodeBean) {
        setJump(appCodeBean);
    }

    private void setJump(UpdateBean appCodeBean) {
        if (UserInfoHelper.getInstance().isFirstStart()) {
            startActivity(GuideWelcomeActivity.callIntent(this));
            finish();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    BaseActionHelper.with(getActivity()).handleAction(ActionHeads.LMActionHome);
                    finish();
                }
            }, 500);

        }
    }

    @Override
    public void getAppCodeFailed() {
        showErrorUi();
    }

    @Override
    public void getProtocolSuccess(ProtocolBean bean) {
        if (null != bean) {
            showPrivacyPop(bean.getRegisterProtocol(), bean.getPrivacyPolicy());
        }
    }
}
